// pages/mine/mine.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const { mpserverless } = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        hasUserInfo: false,
    },

    userLogin() {
        //获取用户身份证
        let openid = getApp().globalData.openId;
        //获取用户信息
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途
            lang: 'zh_CN',
            success: info => {
                //把获取到的信息复制到data中的loginInfo中
                this.setData({ userInfo: info.userInfo, hasUserInfo: true });
                //将用户信息上传到云数据库
                mpserverless.db.collection('userInfo').find({
                    openId:openid
                }).then(res=>{
                    if(res.result == ''){
                        mpserverless.db.collection('userInfo').insertOne({
                            openId: openid,
                            userInfo: info.userInfo
                        }).then(result => {})
                        getApp().globalData.isAuthorized = true;
                        //将已授权存储到storage里面
                        wx.setStorage({
                            key: 'Authorized',
                            data: {
                                isAuthorized: true
                            },
                            success: (result) => { }
                        });
                    }else{
                        getApp().globalData.isAuthorized = true;
                        //将已授权存储到storage里面
                        wx.setStorage({
                            key: 'Authorized',
                            data: {
                                isAuthorized: true
                            },
                            success: (result) => { }
                        });
                    }
                }).catch(e=>{
                    Toast.fail('诶，出现问题！');
                })
            },
            fail: e => {
                Toast.fail('诶，找不到你的信息了！');
            }
        })
    },
    cellYear(){
        Dialog.confirm({
            title: '温馨警示',
            message: '别点了，还没做出来！🤬',
            confirmButtonText:'好的哟',
            cancelButtonText:'就不'
        }).then(() => {
            Toast.success('真乖!');
        }).catch(() => {
            Toast.fail('你不乖!');
        });
    },
    cellLike(){
        Dialog.alert({
            message: '谢谢你夸我帅！我也这么觉得，嘿嘿！',
            confirmButtonText:'真滴帅',
            theme: 'round-button',
          }).then(() => {});
    },
    toVault(){
        let Authorize;
        wx.getStorage({
            key: 'Authorized',
            success: (result)=>{
                Authorize = result.data.isAuthorized;
                if (Authorize) {
                    wx.navigateTo({
                        url: '../vault/vault',
                    });
                } else {
                    Dialog.confirm({
                        title: '乖乖',
                        message: '微信授权登录后才能正常使用该功能噢',
                        confirmButtonText: '去登录',
                        cancelButtonText: '等会再登'
                    });
                }
            },
            fail: (e)=>{
                Dialog.confirm({
                    title: '乖乖',
                    message: '微信授权登录后才能正常使用该功能噢',
                    confirmButtonText: '去登录',
                    cancelButtonText: '等会再登'
                });
            },
        });
    },
    quitLogin(){
        let openid = getApp().globalData.openId;
        Dialog.confirm({
            title: '警告！',
            message: '注销账号后将删除所有记账数据，请谨慎操作！'
        }).then(() => {
            mpserverless.db.collection('userInfo').deleteOne({openId:openid});
            mpserverless.db.collection('account').deleteMany({
                openId:openid
            }).then(res=>{
                this.setData({ hasUserInfo:false});
                wx.removeStorage({key: 'Authorized'});
                wx.removeStorage({key: 'allData'});
                Toast.success('注销成功！');
                getApp().globalData.isFreash = true;
                wx.switchTab({
                    url: '/pages/index/index',
                })
            })
        }).catch(() => {
            Toast.fail('吓我一跳');
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const isAuthorized = getApp().globalData.isAuthorized;
        const userInfo = getApp().globalData.userInfo;
        if(userInfo == ''){
            wx.getStorage({
                key: 'userInfo',
                success: (result)=>{
                    this.setData({
                        hasUserInfo:isAuthorized,
                        userInfo:result.data.userInfo
                    })
                },
            });
        }else{
            this.setData({
                hasUserInfo:isAuthorized,
                userInfo:userInfo
            })
        }
        
    },

    /**
     * 用户点击右上角分享
     */
     onShareAppMessage: function () {
        return {
            title: '快来看帅哥做的记账本!!!',
            path: '/pages/mine/mine',
            // 设置转发的图片
          imageUrl: 'https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/7ab9bb73-6f8d-4e85-8b9e-983561662fed.png',
            // 成功的回调
            success: (res) => {
                wx.showToast({
                    title: '分享成功！',
                    icon: 'success',
                    duration: 1500,
                });
            },
            // 失败的回调
            fail: (res) => {},
            // 无论成功与否的回调
            complete: (res) => {}
          }
    }
})